package com.agenda.client.requisicoes;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.Serializable;
import com.agenda.comum.ObjectEnvioInformacao;
import com.agenda.model.AnexoCompromisso;
import com.agenda.model.Compromisso;
import com.agenda.model.Requisicao;
import com.agenda.model.RequisicaoEnum;
import com.agenda.util.FileUtil;

public class EnviarCompromissoRequisicao extends ObjectEnvioInformacao {

	private Compromisso compromisso;

	public EnviarCompromissoRequisicao(Compromisso compromisso) {
		this.compromisso = compromisso;

	}

	@Override
	public void enviar(OutputStream output) throws Exception {
		super.enviar(output);

		for (AnexoCompromisso anexo : this.compromisso.getAnexos()) {
			if (!anexo.isNoSevidor()) {
				System.out.println("Arquivo atual sendo enviado " + anexo.getNome());
				BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(new File(anexo.getCaminho())));

				BufferedOutputStream bufferOutput = new BufferedOutputStream(output, 1024);

				FileUtil.enviar(bufferOutput, fileInputStream, sck.getInputStream());
				//bufferOutput.flush();

				fileInputStream.close();

				int confirmou = sck.getInputStream().read();
				if (confirmou == 1) {
					System.out.println("Confirmou recebimento do arquivo.");
				}
			}
		}
	}

	@Override
	public Requisicao getRequisicao() {
		return new Requisicao(RequisicaoEnum.ENVIAR_COMPROMISSO.getId());
	}

	@Override
	public Serializable getObjeto() {
		return compromisso;
	}
}
